﻿using AiMoWindows.Compiler;

namespace AiMoWindows.CodeScripts
{

    class CodeBlock
    {
        public string[] Lines { get; private set; }
        public bool ReadOnly { get; }
        public bool IsEmpty => Lines.Length == 0 || Lines.All(x => string.IsNullOrWhiteSpace(x));
        public CodeBlock(IEnumerable<string> lines, bool readOnly = false)
        {
            Lines = lines.ToArray();
            ReadOnly = readOnly;
        }
        public CodeBlock(string content, bool readOnly = false)
        {
            Lines = Utils.Split(Utils.FixNewlines(content));
            ReadOnly = readOnly;
        }
        public void UpdateLines(IEnumerable<string>? lines)
        {
            if (!ReadOnly && lines != null)
                Lines = lines.ToArray();
        }
        public void UpdateLines(string allString)
        {
            Lines = Utils.Split(Utils.FixNewlines(allString));
        }

        public override string ToString()
        {
            return string.Join(Environment.NewLine, Lines);
        }
        public static CodeBlock Default => new CodeBlock(defaultEmptyString, false);

        static readonly string[] defaultEmptyString = new string[] { "" };
    }
}
